<?php
/**
 * （logic） 基础备货JPsku非FBA库存（JP备货）（库存来源：海外仓和国内仓）
 */
namespace App\Logic\SsBasicJp;

use App\Models\Basics\SysPlat;
use App\Models\Inventory\AreaCn;
use App\Models\OverSeasBa\DropshippingInventoryFnsku;
use App\Models\OverSeasBa\InventoryBoxFNSku;
use App\Models\OverSeasBa\RemovaleInventoryFNSku;
use App\Models\OverSeasBa\Warehouse;
use App\Models\SsBasic\JpSkuStock;
use App\Models\SsBasic\JpStockingSku;
use Illuminate\Support\Facades\DB;
class InitJpNotFBAStock
{
    /**
     * 设定：每隔*分钟运行 （JP:not-fba-stock）
     */
    public static function exe(){

        try{
            $areaId = 6; //JP站点
            $warehouse = [];

            //备货JPsku
            JpStockingSku::query()->orderBy('id')
                ->chunk(500, function ($stockingSkus) use (&$warehouse,$areaId){
                    DB::beginTransaction();
                    foreach ($stockingSkus as $stockingSku){
                        //国内库存
                        $instant = AreaCn::getInstant($areaId,$stockingSku->psku_id);
                        $transit = AreaCn::getTransit($areaId,$stockingSku->psku_id);

                        if(!array_key_exists($stockingSku->country_id,$warehouse)){
                            //JP (JP国家ID为：12)
                            $warehouse[$stockingSku->country_id] = Warehouse::getIdsByCountryId($stockingSku->country_id);
                        }
                        $warehouseIds = $warehouse[$stockingSku->country_id];

                        if (intval($stockingSku->plat_id) == SysPlat::PLAT_AMAZON) {
                            //amazon平台(海外仓库存)
                            $amazonPlat = SysPlat::PLAT_AMAZON;
                            $osInventory1 = InventoryBoxFNSku::getInstantAndTransit($warehouseIds,$stockingSku->psku_id,$amazonPlat);
                            $osInventory2 = RemovaleInventoryFNSku::getInstantAndTransit($warehouseIds,$stockingSku->psku_id,$amazonPlat);
                            $osInventory3 = DropshippingInventoryFnsku::getInstantAndTransit($warehouseIds,$stockingSku->psku_id,$amazonPlat);
                        }

                        if (intval($stockingSku->plat_id) == SysPlat::PLAT_RAKUTEN) {
                            //rakuten平台(海外仓库存)
                            $rakutenPlat = SysPlat::PLAT_RAKUTEN;
                            $osInventory1 = InventoryBoxFNSku::getInstantAndTransit($warehouseIds,$stockingSku->psku_id,$rakutenPlat);
                            $osInventory2 = RemovaleInventoryFNSku::getInstantAndTransit($warehouseIds,$stockingSku->psku_id,$rakutenPlat);
                            $osInventory3 = DropshippingInventoryFnsku::getInstantAndTransit($warehouseIds,$stockingSku->psku_id,$rakutenPlat);
                        }

                        $stockingSku->cn_instant_num = $instant;
                        $stockingSku->cn_transit_num = $transit;
                        $stockingSku->os_instant_num = ($osInventory1->instant ?? 0) + ($osInventory2->instant ?? 0) + ($osInventory3->instant ?? 0);
                        $stockingSku->os_transit_num = ($osInventory1->transit ?? 0) + ($osInventory2->transit ?? 0) + ($osInventory3->transit ?? 0);
                        $stockingSku->save();
                        $stockingSku->over_stock = JpStockingSku::getOverStock($stockingSku);
                        $stockingSku->over_stock_value = JpStockingSku::getOverStockValue($stockingSku->over_stock,$stockingSku->unit_price);
                        $stockingSku->save();

                        //备货站点sku库存
                        $skuStock = JpSkuStock::query()->firstOrNew(['area_psku' => $stockingSku->area_psku]);
                        $skuStock->cn_instant_num = $instant;
                        $skuStock->cn_transit_num = $transit;
                        $skuStock->os_instant_num = ($osInventory1->instant ?? 0) + ($osInventory2->instant ?? 0) + ($osInventory3->instant ?? 0);
                        $skuStock->os_transit_num = ($osInventory1->transit ?? 0) + ($osInventory2->transit ?? 0) + ($osInventory3->transit ?? 0);
                        $skuStock->save();
                    }
                    DB::commit();
                });
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
        }
    }
}
